<template>
  <div class="outMore">
    <el-dialog
      title="提示"
      :visible.sync="dialogAllDisable"
      width="540px"
      class="safeDisableDialog"
      style="margin-top: 20vh;">
      <div style="width: 100%; text-align: center; height: 100px; padding-top: 35px;">
        <img :src="disableIcon"/>
      </div>
      <span>确认批量禁用策略</span>
      <span slot="footer" class="dialog-footer">
          <el-button class="clears" @click="dialogAllDisable = false">取 消</el-button>
          <el-button type="primary" style="float: right" @click="disableAllWork()">确 定</el-button>
        </span>
    </el-dialog>
    <el-dialog
      title="提示"
      :visible.sync="dialogAllEnable"
      width="540px"
      class="safeDisableDialog"
      style="margin-top: 20vh;">
      <div style="width: 100%; text-align: center; height: 100px; padding-top: 35px;">
        <img :src="enableIcon"/>
      </div>
      <span>确认批量启用策略</span>
      <span slot="footer" class="dialog-footer">
          <el-button class="clears" @click="dialogAllEnable = false">取 消</el-button>
          <el-button type="primary" style="float: right" @click="enableAllWork()">确 定</el-button>
        </span>
    </el-dialog>
    <el-dialog class="dialogSafeAdd" title="新增策略" :visible.sync="dialogSafeAdd">
      <el-form ref="form" label-width="80px">
        <span id="addNameSpan">*</span>
        <el-form-item class="editForm" label="策略名称:">
          <el-input class="editInput" v-model="addSafeName"></el-input>
        </el-form-item>
        <el-row class="editRow">
          <el-col class="editRowLeft" :span="8">
            <el-input class="leftInput" v-model="addSafeId" placeholder="输入关键字进行过滤"></el-input>
            <div class="grid-content bg-purple treeFather">
              <el-tree
                :data="safeTreeData"
                :props="safeAddprops"
                show-checkbox
                node-key="resource_id"
                ref="tree"
                :filter-node-method="filterNode"
                @check-change="addSafeCheckChange">
              </el-tree>
            </div>
          </el-col>
          <el-col class="editRowRight" :span="16">
            <div>
              <el-table class="addSafeTable" :data="addSafeTime">
                <el-table-column class="tablePadding" width="435px" prop="name">
                  <template slot-scope="scope">
                    <span class="addSafeSpan" v-if="scope.$index === 0">*</span>
                    <div class="block">
                      <span class="demonstration">时段{{scope.$index + 1}}:</span>
                      <el-date-picker
                        class="timeInterval"
                        v-model="addSafeTime[scope.$index].value"
                        size="small"
                        type="datetimerange"
                        range-separator="至"
                        format="yyyy-MM-dd HH:mm:ss"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期">
                      </el-date-picker>
                    </div>
                    <!--<el-input v-model="addSafeTime[scope.$index].name"></el-input>-->
                  </template>
                </el-table-column>
                <el-table-column class="tablePadding">
                  <template  slot-scope="scope">
                    <el-button v-if="scope.$index < 8" class="tableIcon" title="添加" type="text" size="small" @click="addAddTime(scope.$index)"><i class="el-icon-plus"></i></el-button>
                    <el-button v-if="scope.$index > 0" class="tableIcon" title="删除" type="text" size="small" @click="addDeleteRow(scope.$index)"><i class="el-icon-minus"></i></el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <div class="notes">注: <span id="editAsterisk">*</span> 为必填项</div>
        <el-button class="clears" @click="dialogSafeAdd = false">取 消</el-button>
        <el-button class="bottonConfirm" type="primary" @click="addSafeTimeEnd">确 定</el-button>
      </div>
    </el-dialog>
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item class="handItem" label="安全策略列表">
        <el-input class="handInput" v-model="siteId"></el-input><i @click="searchWork" class="el-icon-search search"></i>
        <el-button v-show="returnShow" class="indieWidth Batchdisable" type="primary" @click="outlineWork">
          重置
        </el-button>
      </el-form-item>
      <el-form-item class="handItem" style="float: right">
        <el-button class="indieWidth" type="primary" @click="addDialog">
          <img :src="addsite"/>新增策略
        </el-button>
        <el-button class="indieWidth clear" type="primary" @click="allDelete">
          <img :src="whitedelete"/>批量删除
        </el-button>
        <el-button class="indieWidth" type="primary" @click="showEnable">
          <img :src="whiteenable"/>批量启用
        </el-button>
        <el-button class="indieWidth Batchdisable" type="primary" @click="showDisable">
          <img :src="whitedisable"/>批量禁用
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import axios from 'axios'

  import addsite from '@/assets/mSiteAdmin/add.png'
  import whitedelete from '@/assets/mSiteAdmin/whitedelete.png'
  import whiteenable from '@/assets/mSiteAdmin/whiteenable.png'
  import whitedisable from '@/assets/mSiteAdmin/whitedisable.png'
  import disableIcon from '@/assets/mSiteAdmin/disable-icon.png'
  import enableIcon from '@/assets/mSiteAdmin/enable-icon.png'
  export default {
    props: ['deleteids'],
    data() {
      return {
        addsite,
        whitedelete,
        whiteenable,
        whitedisable,
        disableIcon,
        enableIcon,
        returnShow: false,
        dialogSafeAdd: false,
        dialogAllDisable: false,
        dialogAllEnable: false,
        addSafeName: '',
        addSafeTime: [{ value: '' }],
        addSafeTree: [],
        addSafeId: '',
        safeTreeData: [],
        safeAddprops: {
          label: 'name'
        },
        siteId: '',
        // 新增安全时段请求内容
        increaseSafeBody: {
          access_token: localStorage.getItem('access_token'),
          type: 'add_safetytime',
          pid: localStorage.getItem('num'),
          data: {
            status: 1,
            name: '',
            equips: [],
            times: []
          }
        },
        // 删除安全时段
        deleteSiteBody: {
          access_token: localStorage.getItem('access_token'),
          type: 'del_safetytime',
          pid: localStorage.getItem('num'),
          ids: ''
        },
        // 启用安全时段
        safeEnable: {
          access_token: localStorage.getItem('access_token'),
          type: 'safetytime_enable',
          pid: localStorage.getItem('num'),
          ids: []
        },
        // 禁用安全时段
        safeDisable: {
          access_token: localStorage.getItem('access_token'),
          type: 'safetytime_disable',
          pid: localStorage.getItem('num'),
          ids: []
        },
        // 获取组织树
        safeTree: {
          access_token: localStorage.getItem('access_token'),
          type: 'resource_getall',
          pid: localStorage.getItem('num'),
          ids: ['']
        }
      }
    },
    watch: {
      addSafeId(val) {
        this.$refs.tree.filter(val)
      }
    },
    methods: {
      // 搜索
      filterNode(value, data) {
        if (!value) return true
        return data.name.indexOf(value) !== -1
      },
      searchWork() {
        if (this.siteId === '') {
          this.$message({
            message: '请输入安全策略名称!',
            type: 'error'
          })
        } else {
          this.returnShow = true
          this.$emit('addSafeName', this.siteId)
        }
      },
      outlineWork() {
        this.returnShow = false
        this.siteId = ''
        this.$emit('addSafeName', this.siteId)
      },
      // 新增安全时段
      addDialog() {
        this.dialogSafeAdd = true
        this.addSafeTime = []
        this.addSafeName = ''
        this.addSafeId = ''
        //        this.$refs.tree.setCheckedKeys([])
        this.addAddTime()
        axios.post('/project', this.safeTree, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.safeTreeData = []
          this.safeTreeData.push(res.data.datas)
        })
          .catch(function(error) {
            console.log(error)
          })
      },
      addAddTime() {
        if (this.addSafeTime.length < 9) {
          this.addSafeTime.push({ value: '' })
        }
      },
      addDeleteRow(index) {
        if (this.addSafeTime.length > 1) {
          this.addSafeTime.splice(index, 1)
        }
      },
      addSafeCheckChange(data, checked, indeterminate) {
        this.addSafeTree = this.$refs.tree.getCheckedKeys()
      },
      handleNodeClick(data) {
      },
      // 删除按钮事件
      allDelete() {
        if (this.deleteids.length === 0) {
          this.$message({
            showClose: true,
            message: '请勾选要批量删除的策略！',
            type: 'error'
          })
        } else {
          this.$confirm('确认批量删除策略？', '提示', {
            cancelButtonClass: 'userCancel',
            confirmButtonClass: 'userConfirm',
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.batchDelete()
          }).catch(() => {
          })
        }
      },
      // 批量删除操作
      batchDelete() {
        this.deleteSiteBody.ids = this.deleteids
        axios.post('/safetytime', this.deleteSiteBody, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.$emit('addSafeName', this.siteId)
        })
          .catch(function(error) {
            console.log(error)
          })
      },
      // 新增安全时段按钮事件
      addSafeTimeEnd() {
        if (!this.addSafeName) {
          this.$message.error('请输入策略名称')
        } else {
          this.increaseSafeBody.data.name = this.addSafeName
          if (this.addSafeTree.length === 0) {
            this.$message.error('请勾选测点树')
          } else {
            this.increaseSafeBody.data.equips = this.addSafeTree
            for (let i = 0; i < this.addSafeTime.length; i++) {
              if (!this.addSafeTime[i].value) {
                this.$message.error('第' + (i + 1) + '个时段未正常填写')
                return
              } else {
                this.increaseSafeBody.data.times = []
                for (let x = 0; x < this.addSafeTime.length; x++) {
                  if (this.increaseSafeBody.data.times.length < this.addSafeTime.length) {
                    this.increaseSafeBody.data.times.push({
                      begin_time: this.addSafeTime[x].value[0],
                      end_time: this.addSafeTime[x].value[1],
                      week: ['1']
                    })
                  }
                }
              }
            }
            axios.post('/safetytime', this.increaseSafeBody, {
              headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
              }
            }).then((res) => {
              if (res.data.errcode === '00000') {
                this.addSafeName = ''
                this.$refs.tree.setCheckedKeys([''])
                this.$emit('addSafeName', this.siteId)
                this.dialogSafeAdd = false
              } else {
                this.$message.error(res.data.errmsg)
              }
            })
              .catch(function(error) {
                console.log(error)
              })
          }
        }
      },
      // 启用
      showEnable(index, row) {
        if (this.deleteids.length === 0) {
          this.$message({
            showClose: true,
            message: '请勾选要批量启用的策略！',
            type: 'error'
          })
        } else {
          this.dialogAllEnable = true
          this.safeEnable.ids = this.deleteids
        }
      },
      enableAllWork() {
        axios.post('/safetytime', this.safeEnable, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.dialogAllEnable = false
          this.safeEnable.ids = []
          this.$emit('addSafeName', this.siteId)
        })
          .catch(function(error) {
            console.log(error)
          })
      },
      // 禁用
      showDisable(index, row) {
        if (this.deleteids.length === 0) {
          this.$message({
            showClose: true,
            message: '请勾选要批量禁用的策略！',
            type: 'error'
          })
        } else {
          this.dialogAllDisable = true
          this.safeDisable.ids = this.deleteids
        }
      },
      disableAllWork() {
        axios.post('/safetytime', this.safeDisable, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.dialogAllDisable = false
          this.safeDisable.ids = []
          this.$emit('addSafeName', this.siteId)
        })
          .catch(function(error) {
            console.log(error)
          })
      }
    }
  }
</script>

<style scoped>
  .outMore{
    width: 100%;
    height: 50px;
    background-color: #ffffff;
    padding-top: 5px;
    padding-left: 20px;
    border-bottom: 1px solid #e0e0e0;
  }
  /deep/ .search{
    position: absolute;
    margin-top: 13px;
    margin-left: -30px;
    cursor: pointer;
    color: #0072F3;
  }
  /deep/ .outline {
    position: absolute;
    margin-top: 13px;
    margin-left: -25px;
    cursor: pointer;
    color: #D05133;
  }
  .Batchdisable{
    background-color: #D05133 !important;
    border: 1px solid  #D05133 !important;
  }
  /deep/ label {
    font-weight: normal;
    color: #69727E;
  }
  /deep/ .handItem {
    margin-bottom: 5px!important;
  }
  /deep/ .indieWidth {
    padding: 6px 8px;
  }
  /deep/ .reset {
    padding: 6px 16px;
    background-color: #526D89;
    border-color: #526D89 ;
    color: #fff;
  }
  /deep/ .el-button + .el-button {
    margin-left: 5px;
  }
  /deep/ .el-button--primary {
    height: 30px;
    font-size: 14px;
    padding: 6px 16px;
    background: #0072F3;
    border-color: #0072F3;
  }
  /deep/ .el-input__inner {
    height: 30px;
    line-height: 30px;
    width: 360px;
  }
  /deep/ .handInput {
    width: 365px;
  }
  /deep/ .userCancel {
    border: #333B3D;
  }
  /deep/ .clear{
    padding: 6px 16px;
    background-color: #526D89;
    border-color: #526D89 ;
    color: #fff;
  }
  /deep/ .export {
    background: #fff;
    border: 1px solid #0072F3;
    color: #0072F3;
  }
  /deep/ .el-icon-delete {
    height: 16px;
    line-height: 12px;
  }
  /deep/ .el-icon-delete:before {
    font-size: 16px;
    color: #fff;
  }
  /deep/ .el-message-box{
    width: 540px;
  }
  /deep/ .el-message-box__header{
    background-color: #E3E6EE;
    padding: 10px 15px;
  }
  /deep/ .el-message-box__title{
    font-size: 18px;
    font-weight: Bold;
    color: #323842;
  }
  /deep/ .el-message-box__status{
    right: 247px;
    top: 65px;
    font-size: 48px !important;
  }
  /deep/ .el-message-box__status.el-icon-warning{
    color: #E53D41;
  }
  /deep/ .el-message-box__content{
    height: 182px;
  }
  /deep/ .el-message-box__message p {
    margin: 0;
    font-size: 18px;
    color: #323842;
    padding-top: 96px;
    text-indent: 0px;
    text-align: center;
  }
  /deep/ .el-message-box__headerbtn{
    top: 9px;
    font-size: 18px;
  }
  /deep/ .el-message-box__btns{
    padding: 10px 15px 7px ;
    border-top: 1px solid #e8eaf1;
  }
  /deep/ .dialogSafeAdd > .el-dialog > .el-dialog__body {
    padding: 0px;
  }
  /deep/ .dialogSafeAdd > .el-dialog > .el-dialog__footer {
    height: 50px;
  }
  /deep/ .el-dialog {
    width: 800px;
  }
  /deep/ .editForm {
    padding: 10px 20px;
    margin: 0px;
  }
  /deep/ .editRowLeft {
    background: #EAEDF1;
    border: 1px solid #E4E5E7;
    height: 499px;
  }
  /deep/ .treeFather {
    overflow: auto;
    height: 447px;
  }
  /deep/ .notes {
    float: left;
    height: 30px;
    line-height: 30px;
    font-size: 14px;
    color: #69727E;
  }
  /deep/ #editAsterisk {
    color: #E53D41;
  }
  /deep/ .clears{
    padding: 6px 16px;
    height:30px;
    background-color: #526D89;
    border-color: #526D89 ;
    color: #fff;
    float: right;
    margin-left: 10px;
  }
  /deep/ .bottonConfirm {
    padding: 6px 16px;
    float: right;
    border:1px solid #0072F3;
    background: #0072F3;
  }
  /deep/ .leftInput > .el-input__inner {
    height: 30px;
    margin:10px 20px;
    width: 220px;
  }
  /deep/ .editSearch{
    position: absolute;
    margin-top: 18px;
    margin-left: -45px;
    cursor: pointer;
    color: #0072F3;
  }
  /deep/ .addEditIcon {
    color: #0072F3;
    font-size: 16px;
    cursor: pointer;
  }

  /deep/ .addSafeTable {
    height: 495px;
    overflow-y: auto;
  }
  /deep/ .addSafeTable > .el-table__header-wrapper{
    height: 0px;
  }
  /deep/ .addSafeTable > .el-table__body-wrapper > .el-table__body > tbody > .el-table__row > .el-table td, .el-table th{
    padding: 5px 0px;
  }
  /deep/ .el-icon-minus {
    color: #D05133;
  }
  /deep/ .timeInterval {
    height: 30px;
    width: 365px;
    float: right;
  }
  #addNameSpan {
    float: left;
    margin: 20px 0px 0px 14px;
    color: #E53D41;
  }
  .addSafeSpan {
    float: left;
    margin: 6px 0px 0px 0px;
    color: #E53D41;
  }
  /deep/ .safeDisableDialog > .el-dialog > .el-dialog__body {
    text-align: center;
    font-size: 18px;
    height: 180px;
    border-bottom: 1px solid #e8eaf1;
  }
  /deep/ .safeDisableDialog > .el-dialog > .el-dialog__footer {
    height: 60px;
  }
  /deep/ .safeDisableDialog > .el-dialog > .el-dialog__footer > .dialog-footer > button {
    height: 30px;
    font-size: 14px;
    padding: 6px 16px;
  }
</style>
